using System;
using System.Data;
using System.Windows.Forms;
using System.Collections;
using VNPT.Data;

namespace VNPT.DoiSoat
{
    public partial class frmDbfCreateTable : DevExpress.XtraEditors.XtraForm
    {

        public bool flg_createTb = false;
        //public string TableName = String.Empty;
        private bool isInitializingComponent;
        public ArrayList Fields;
        DbNetData _db;

        public frmDbfCreateTable()
        {
            InitializeComponent();
            this.Shown+=(a,e)=>
                {
                    this.txtTableName.Text = _TableName;
                    LoadDbfFields();
                };

          //  ReLoadForm(false);
        }
        public frmDbfCreateTable(DbNetData db)
        {
            _db = db;
        }
        string _fileName;
        string _TableName;
        public frmDbfCreateTable(string fileName, string TableName):this()
        {
            _fileName = fileName;
            _TableName = TableName;
        }

        private void cmdAdd_Click(object sender, EventArgs e)
        {
            gridView1.AddNewRow();
        }

        private void cmdCreate_Click(object sender, EventArgs e)
        {
            string Error = String.Empty;
            Error = (chkDbf.Checked ?
                  Dbf.CreateTableFromDbf(txtTableName.Text, Fields,_db)
                : Dbf.CreateTable(txtTableName.Text, gridView1, _db));

            if (Error == "")
            {
                flg_createTb = true;
                this.Close();
            }
            else
            {
                flg_createTb = false;
                DevExpress.XtraEditors.XtraMessageBox.Show("Error: " + Error);
            }
        }




        private void LoadDbfFields()
        {
            cmdAdd.Enabled = (chkDbf.Checked ? true : false);
            cmdRemove.Enabled = (chkDbf.Checked ? true : false);
            gridView1.OptionsBehavior.Editable = (chkDbf.Checked ? true : false);

            DataTable Dt = new DataTable();

            Dt = FoxUltil.GetDataField(_fileName, out Fields);
            gridControl1.DataSource = Dt;
        }

        private void cmdClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmDBFCreateTable_FormClosed(object sender, FormClosedEventArgs e)
        {
            _TableName = txtTableName.Text.Trim();
        }

        private void frmDBFCreateTable_Load(object sender, EventArgs e)
        {
            flg_createTb = false;

            this.txtTableName.Text = _TableName;

            if (txtTableName.Text != "")
            {
                chkDbf.Checked = true;
                cmdAdd.Enabled = false;
                cmdRemove.Enabled = false;
            }
            else
            {
                chkOptional.Checked = true;
                chkDbf.Enabled = false;
                cmdAdd.Enabled = true;
                cmdRemove.Enabled = true;
            }
        }

        private void cmdRemove_Click(object sender, EventArgs e)
        {
            gridView1.DeleteRow(gridView1.FocusedRowHandle);
        }

        private void chkOptional_CheckedChanged(object sender, EventArgs e)
        {
            if (chkOptional.Checked)
            {
                gridView1.OptionsBehavior.Editable = true;
                cmdAdd.Enabled = true;
                cmdRemove.Enabled = true;
            }
            else
            {
                gridView1.OptionsBehavior.Editable = false;
                cmdAdd.Enabled = false;
                cmdRemove.Enabled = false;
            }
        }
    }
}